/*
 * 分享朋友圈插件
 * 依赖的5+模块列表
 * Downloader
 * IO
 * Native.js
 * */
(function() {
	var jushiShare = {
		share: function(imgs,href,content) {
			var self = this;
			if(href){
				this._down(imgs, function(data) {
					self._shareLink(href,content,data);
				});
			}else{
				this._downImgs(imgs, function(data) {
					self._sharePics(data);
				});
			}
			
		},
		_down: function(img, callback) {
			var pics = [];
			mui.plusReady(function() {
				plus.nativeUI.showWaiting('获取图片...');
				plus.downloader.createDownload(img, {
						method: "GET",
						filename: '_downloads/image/' + md5(img)+'.jpg',
						timeout: 10,
					}, function(download, status) {
						if(status === 200) {
							pics.push(download.filename);
							//alertHTML(pics)
							callback(pics);
							plus.nativeUI.closeWaiting();
						} else {
							new Error(img + 'download error');
						}
					}).start();
			});
		},
		_downImgs: function(imgs, callback) {
			var pics = [];
			mui.plusReady(function() {
				plus.nativeUI.showWaiting('获取图片...');
				imgs.forEach(function(imgUrl) {
					var imgUrl = imgUrl+"@!auto600";
					plus.downloader.createDownload(imgUrl, {
						method: "GET",
						filename: '_downloads/image/' + md5(imgUrl)+'.jpg',
						timeout: 10,
					}, function(download, status) {
						if(status === 200) {
							pics.push(download.filename);
							if(pics.length === imgs.length) {
								callback(pics);
								plus.nativeUI.closeWaiting();
							}
						} else {
							new Error(imgUrl + 'download error');
						}
					}).start();
				});
			});
		},
		_shareLink : function(href,content,thumbs){
			var self = this;
			var msg={};
			msg.href = href;
			msg.pictures = thumbs;
			msg.thumbs = thumbs;
			msg.content = content; 
			msg.title = content;
			plus.share.sendWithSystem?plus.share.sendWithSystem(msg, function(){
				log('Success');
			}, function(e){
				mui.toast('取消分享 ');
			}):this.shareSystemNativeJS();
		},
		_sharePics: function(pics) {
			var self = this;
			var msg={};
			msg.thumbs = pics
			msg.pictures = pics
			plus.share.sendWithSystem?plus.share.sendWithSystem(msg, function(){
				log('Success');
			}, function(e){
				mui.toast('取消分享 ');
			}):this.shareSystemNativeJS();
		},
		shareSystemNativeJS: function(){
			if(plus.os.name!=='Android'){
				plus.nativeUI.alert('此平台暂不支持系统分享功能!');
				return;
			}
			var intent=new Intent(Intent.ACTION_SEND);
			if(pic&&pic.realUrl){
				var p = '';
				p = pic.realUrl;
				if(p.substr(0,7)==='file://'){
					p=p.substr(7);
				}else if(p.sub(0)!=='/'){
					p=plus.io.convertLocalFileSystemURL(p);
				}
			}
			var f = new File(p);
			var uri = Uri.fromFile(f);
			if(f.exists()&&f.isFile()){
				console.log('image/*');
				intent.setType('image/*');
				intent.putExtra(Intent.EXTRA_STREAM,uri);
			}else{
				console.log('text/plain');
				intent.setType('text/plain');
			}
			intent.putExtra(Intent.EXTRA_SUBJECT,'HelloH5');
			intent.putExtra(Intent.EXTRA_TEXT,sharecontent.value);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			main.startActivity(Intent.createChooser(intent,'系统分享HelloH5'));
		}
	}
	window.jushiShare = jushiShare;
})();

// 使用示例
//jushiShare.share(vm.goodsInfo.pics)
//jushiShare.share(vm.goodsInfo.pics[0],'http://www.baidu.com','dddddd')